//
//  subViewForIndicationList.m
//  HondaPro_2
//
//  Created by Eric on 13-7-9.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "subViewForIndicationList.h"
#import "UserDefinedCellForSubViewForIndicationList.h"

@interface subViewForIndicationList ()

//创建控件
-(void)createStaticLabels;
-(void)createTableView;

//刷新数据
-(void)freshenViewDis;

@end

@implementation subViewForIndicationList
@synthesize tableViewDis = _tableViewDis;

@synthesize arrOriginalDataForServiceType = _arrOriginalDataForServiceType;
@synthesize arrOriginalDataForRepairType = _arrOriginalDataForRepairType;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self createStaticLabels];
        [self createTableView];
        
        self.arrOriginalDataForRepairType = [[HelpClass returnTheDataByJsonFileAndJustForTestWithFileName:@"indication_repairType" andType:@"json"] objectForKey:@"repairTypeList"];
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    [self freshenViewDis];
}

#pragma mark - 创建控件

-(void)createStaticLabels
{
    NSArray *arrForAllKeys = [NSArray arrayWithObjects:
                              @"bottomLbl",
                              @"indicationnumber",
                              @"indicationContent",
                              @"serviceType",
                              @"repairType", nil];
    
    NSDictionary *dicForStaticLabelsRect = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [NSValue valueWithCGRect:CGRectMake(0, 0, self.frame.size.width, 50)],@"bottomLbl",
                                            [NSValue valueWithCGRect:CGRectMake(0, 0, 80, 50)],@"indicationnumber",
                                            [NSValue valueWithCGRect:CGRectMake(81, 0, 100+120, 50)],@"indicationContent",
                                            [NSValue valueWithCGRect:CGRectMake(182+120, 0, 100-20, 50)],@"serviceType",
                                            [NSValue valueWithCGRect:CGRectMake(283+100, 0, self.frame.size.width-283-100, 50)],@"repairType", nil];
    
    NSDictionary *dicForStaticLabelsText = [NSDictionary dictionaryWithObjectsAndKeys:
                                            @"",@"bottomLbl",
                                            @"序号",@"indicationnumber",
                                            @"作业指示",@"indicationContent",
                                            @"服务类型",@"serviceType",
                                            @"修理类型",@"repairType", nil];
    
    for (int i = 0; i<[arrForAllKeys count]; i++)
    {
        NSString *strKeys = [arrForAllKeys objectAtIndex:i];
        CGRect rect = (CGRect)[[dicForStaticLabelsRect objectForKey:strKeys] CGRectValue];
        UILabel *lbl = [[UILabel alloc] init];
        lbl.frame = rect;
        if ([strKeys isEqualToString:@"bottomLbl"])
        {
            //            lbl.backgroundColor = [UIColor grayColor];
            //将灰色背景色修改成浅灰色
            lbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
        }
        else
        {
            //将灰色背景色修改成浅灰色
            lbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
            
//            lbl.backgroundColor = [UIColor redColor];
        }
        lbl.textColor = [UIColor grayColor];
        lbl.font = [UIFont systemFontOfSize:16];
        lbl.text = [dicForStaticLabelsText objectForKey:strKeys];
        [self addSubview:lbl];
        
        if ([strKeys isEqualToString:@"indicationnumber"])
        {
            lbl.textAlignment = UITextAlignmentCenter;
        }
    }
}

-(void)createTableView
{
    self.tableViewDis = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.frame.size.width, self.frame.size.height-50)];
    self.tableViewDis.backgroundColor = [UIColor clearColor];
    
    self.tableViewDis.dataSource = self;
    self.tableViewDis.delegate = self;
    
    [self addSubview:self.tableViewDis];
}

#pragma mark - 刷新数据

-(void)freshenViewDis
{
    [self.tableViewDis reloadData];
}

#pragma mark - tableView dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSInteger nRowSum = 0;
    if (modelIns.nRowForResults != -1 && modelIns.nRowForResults < [modelIns.arrForResults count])
    {
        nRowSum = [[[modelIns.arrForResults objectAtIndex:modelIns.nRowForResults] objectForKey:@"indicationList"] count];
    }
    
    return 3+nRowSum;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UserDefinedCellForSubViewForIndicationList *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (nil == cell)
    {
        cell = [[UserDefinedCellForSubViewForIndicationList alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    //cell的风格是：浅浅灰色和浅灰色间隔出现，第一行是浅灰色
    NSInteger row = [indexPath row];
    switch (row%2)
    {
        case 0:
            //浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
            break;
            
        default:
            break;
    }
    
    /*
     此处的操作是：
     （1）如果陈述号的行号选择 是 －1，不显示数据，否则显示对应的数据
     */
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSArray *arrTmp = modelIns.arrForResults;
    
    if (modelIns.nRowForResults != -1 && modelIns.nRowForResults < [arrTmp count])
    {
        //取出陈述号对应的作业指示进行显示
        NSDictionary *dicTmp = [arrTmp objectAtIndex:modelIns.nRowForResults];
        NSArray *arrIndicationTmp = [dicTmp objectForKey:@"indicationList"];
        
        if (row<[arrIndicationTmp count])
        {
            NSDictionary *dicIndicationTmp = [arrIndicationTmp objectAtIndex:row];
            for (int i = 0; i<[cell.arrForAllLabelsKeysOnCell count]; i++)
            {
                if ([[cell.arrForAllLabelsKeysOnCell objectAtIndex:i] isEqualToString:@"repairTypeName"])
                {
                    //显示维修分类的name
                    ((UILabel *)[cell.dicForAllLabelsOnCell objectForKey:[cell.arrForAllLabelsKeysOnCell objectAtIndex:i]]).text = [self getNameByRepairId:[dicIndicationTmp objectForKey:[cell.arrForAllLabelsKeysOnCell objectAtIndex:i]]];
                }
                else if ([[cell.arrForAllLabelsKeysOnCell objectAtIndex:i] isEqualToString:@"serviceType"])
                {
                    //显示服务类型的name
                    ((UILabel *)[cell.dicForAllLabelsOnCell objectForKey:[cell.arrForAllLabelsKeysOnCell objectAtIndex:i]]).text = [self getNameByServiceId:[dicIndicationTmp objectForKey:[cell.arrForAllLabelsKeysOnCell objectAtIndex:i]]];
                }
                else
                {
                    ((UILabel *)[cell.dicForAllLabelsOnCell objectForKey:[cell.arrForAllLabelsKeysOnCell objectAtIndex:i]]).text = [dicIndicationTmp objectForKey:[cell.arrForAllLabelsKeysOnCell objectAtIndex:i]];
                }
            }
            //cell可选中
            cell.selectionStyle = UITableViewCellSelectionStyleGray;
        }
        else
        {
            for (int i = 0; i<[cell.arrForAllLabelsKeysOnCell count]; i++)
            {
                ((UILabel *)[cell.dicForAllLabelsOnCell objectForKey:[cell.arrForAllLabelsKeysOnCell objectAtIndex:i]]).text = @"";
            }
            //cell不可选中
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    }
    else
    {
        for (int i = 0; i<[cell.arrForAllLabelsKeysOnCell count]; i++)
        {
            ((UILabel *)[cell.dicForAllLabelsOnCell objectForKey:[cell.arrForAllLabelsKeysOnCell objectAtIndex:i]]).text = @"";
        }
        
        //cell不可选中
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

#pragma mark - tableView delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    
    //在此 将业务模型中对应的属性 设定为 当前选中的为第几行
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    if (modelIns.nRowForResults == -1 || row>= [[[modelIns.arrForResults objectAtIndex:modelIns.nRowForResults] objectForKey:@"indicationList"] count])
    {
        modelIns.nRowForIndicationList = -1;
    }
    else
    {
        modelIns.nRowForIndicationList = row;
    }
    
    //设置其他页面选择行 为 默认选择行 －1
    modelIns.nRowForRepairItemList = -1;
    modelIns.nRowForPartList = -1;
    modelIns.nRowForOtherChargeList = -1;
    
    //刷新数据，通过发送消息完成
    
    //给 作业指示 类 发送消息
    [[NSNotificationCenter defaultCenter] postNotificationName:REPAIR_ITEM_LIST_MSG_ object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:PART_LIST_MSG object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:OTHER_CHARGE_LIST_MSG object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:ADD_BTN_MSG_ object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:MODIFY_BTN_MSG_ object:nil];
}

//根据ID取名字
-(NSString *)getNameByServiceId:(NSString *)serviceId
{
    for (int i = 0; i<[self.arrOriginalDataForServiceType count]; i++)
    {
        NSString *strId = [[self.arrOriginalDataForServiceType objectAtIndex:i] objectForKey:@"serviceTypeId"];
        NSString *strName = [[self.arrOriginalDataForServiceType objectAtIndex:i] objectForKey:@"serviceTypeName"];
        if ([serviceId isEqualToString:strId])
        {
            return strName;
        }
    }
    return @"";
}

-(NSString *)getNameByRepairId:(NSString *)RepairId
{
    for (int i = 0; i<[self.arrOriginalDataForRepairType count]; i++)
    {
        NSString *strId = [[self.arrOriginalDataForRepairType objectAtIndex:i] objectForKey:@"repairTypeId"];
        NSString *strName = [[self.arrOriginalDataForRepairType objectAtIndex:i] objectForKey:@"repairTypeName"];
        if ([RepairId isEqualToString:strId])
        {
            return strName;
        }
    }
    return @"";
}

@end
